<?php

namespace App\Console\Commands;

use App\Api\Models\Config;
use App\Models\GoodOrder;
use App\Models\OrderCommission;
use App\Models\ServiceOrder;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Database\QueryException;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Throwable;

class Order extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order_handle';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每分钟检测数据(处理签收订单与分润状态更新)';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->checkOrders();
        $this->updateCommissionOrders();
        return 0;
    }

    /**
     * 处理签收订单,到期自动签收
     */
    public function checkOrders(){
        DB::beginTransaction();
        try{
            $confirmDay = Config::getName("confirm_hours");
            $confirmSeconds = $confirmDay*3600*24;
            $orders = GoodOrder::query()
                ->where("state",2)
                ->get();
            foreach ($orders as $order){
                $end = Carbon::parse($order->send_time)->addSeconds($confirmSeconds);
                if (now()->gte($end)){
                    $gd = GoodOrder::whereId($order->id)->first();
                    $gd->finish_time = now()->format("Y-m-d H:i:s");
                    $gd->state = 3;
                    $gd->save();
                }
            }
            DB::commit();
        } catch(QueryException|Throwable|Exception $e) {
            $this->info("处理签收订单,到期自动签收错误!----------------------------------------");
            $this->info($e->getMessage());
            $this->info("处理签收订单,到期自动签收错误!----------------------------------------");
            DB::rollback();
        }
    }

    /**
     * 检测遗漏未结算表,自动更新结算状态
     */
    public function updateCommissionOrders(){
        DB::beginTransaction();
        try{
            $finishDay = Config::getName("finish_day");
            $finishSeconds = $finishDay*3600*24;
            $list = OrderCommission::query()->where("is_settle",0)->get();
            foreach ($list as $item){
                $end = Carbon::parse($item->created_at)->addSeconds($finishSeconds);
                if (now()->gte($end)){
                    if ($item->type){
                        $order = GoodOrder::whereOrderNo($item->order_id)->first();
                        if ($order){
                            if ($order->state==3||$order->state==4){
                                OrderCommission::updateCommissionOrders($item->order_id,"商品订单分成");
                            }
                        }
                    }else{
                        $order = ServiceOrder::whereOrderId($item->order_id)->first();
                        if($order){
                            if ($order->status==2||$order->status==3){
                                OrderCommission::updateCommissionOrders($item->order_id,"洗车订单分成");
                            }
                        }
                    }
                }
            }
            DB::commit();
        } catch(QueryException|Throwable|Exception $e) {
            $this->info("更新结算表状态 错误!----------------------------------------");
            $this->info($e->getMessage());
            $this->info("更新结算表状态 错误!----------------------------------------");
            DB::rollback();
        }
    }

}
